<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends User_Controller
{

	
	function __construct()
	{
		parent::__construct();
        $this->smarty->assign("current_class", $this->router->fetch_class());
        $this->smarty->assign("current_method",$this->router->fetch_method());
		$this->uid = $this->user_data['id'];
		if( $this->logged_in==0) {header("Location:/");exit;}
	}	
	
	public function orders()
	{
		$this->offset = 10;
		$this->load->model('User_center_model');
		//订单状态-1取消0待支付1已支付2已发货3已收货
		$total_count = $this->User_center_model->get_order_count_by_status($this->user_data['id']);
		$finish_count = $this->User_center_model->get_order_count_by_status($this->user_data['id'],3);
		$cancel_count = $this->User_center_model->get_order_count_by_status($this->user_data['id'],0);
		$result = $this->User_center_model->get_order_list_by_uid(0,$this->offset,$this->user_data['id'],1,-1,"","","");
		$this->smarty->assign("order", $result['result']);
		//分页
		$this->load->library('pagination');
		$page_config['base_url'] = '/order/index/';
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("finish_count", $finish_count);
		$this->smarty->assign("cancel_count", $cancel_count);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("menu", "orders");
		$this->smarty->display("user/order.html");
	}/**
	 * 直播订单列表
	 */
	public function zhibo_orders()
	{
		$this->offset = 10;

		$this->load->model('User_center_model');
		$result = $this->User_center_model->get_order_list_by_uid(0,$this->offset,$this->user_data['id'],2,-1,"","","");
		$this->smarty->assign("order", $result['result']);
		//分页
		$this->load->library('pagination');
		$page_config['base_url'] = '/order/zhibo/';
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("menu", "orders");
		$this->smarty->display("user/order.html");
	}
	/**
	 * 直播订单列表
	 */
	public function banke_orders()
	{
		$this->offset = 10;

		$this->load->model('User_center_model');
		$result = $this->User_center_model->get_order_list_by_uid(0,$this->offset,$this->user_data['id'],3,-1,"","","");
		$this->smarty->assign("order", $result['result']);
		//分页
		$this->load->library('pagination');
		$page_config['base_url'] = '/order/banke/';
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("menu", "orders");
		$this->smarty->display("user/order.html");
	}
	
	/**
	 * 视频课
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function video_course($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0)
	{
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
		}
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 1, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 10;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->model('User_center_model');
		$result = $this->User_center_model->get_anchors_course_list($data['limit'], $offset,$data);
		
		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->User_center_model->get_course_cate_subject($cate_id);
		$cate = $this->User_center_model->get_course_cate(1);
		$this->load->library('pagination');
		$page_config['base_url'] = '/user/course/video/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$page_config['uri_segment'] = 10;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("time", time());
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("menu", "course");
		$this->smarty->display("user/video_course_list.html");
	}

	/**
	 * 直播list
	 * @param  integer $status  [description]
	 * @param  integer $sort    [description]
	 * @param  integer $asc_des [description]
	 * @param  integer $search  [description]
	 * @param  integer $kemu    [description]
	 * @param  integer $limit   [description]
	 * @return [type]           [description]
	 */
	function zhibo_course($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model("User_center_model");
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 2, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 1;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->User_center_model->get_anchors_course_list($data['limit'], $offset,$data);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->User_center_model->get_course_cate_subject($cate_id);
		$cate = $this->User_center_model->get_course_cate(2);
		$this->load->library('pagination');
		$page_config['base_url'] = '/user/zhibo_course/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->display("user/zhibo_course_list.html");
	}

	/**
	 * 线下list
	 * @param  integer $status  [description]
	 * @param  integer $sort    [description]
	 * @param  integer $asc_des [description]
	 * @param  integer $search  [description]
	 * @param  integer $kemu    [description]
	 * @param  integer $limit   [description]
	 * @return [type]           [description]
	 */
	function line_course($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model("User_center_model");
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 3, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '视频列表');
		$offset = 1;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$result = $this->User_center_model->get_anchors_course_list($data['limit'], $offset,$data);
		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->User_center_model->get_course_cate_subject($cate_id);
		$cate = $this->User_center_model->get_course_cate(2);
		$this->load->library('pagination');
		$page_config['base_url'] = '/user/zhibo_course/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);
		$this->smarty->assign("menu", "course");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->display("user/line_course_list.html");
	}
	
	public function classes()
	{
		//获取班级
		$this->load->model('User_center_model');
		$classes = $this->User_center_model->get_class_by_uid($this->user_data['id']);
		$this->smarty->assign("classes", $classes);
		$this->smarty->assign("menu", "classes");
		$this->smarty->display("user/classes.html");
	}
	
	public function classes_detail($id){
		//班级详情
		$this->load->model('User_center_model');
		$class_info = $this->User_center_model->get_class_by_id($id);
		$this->smarty->assign("class_info", $class_info);

		//获取班级学员
		$this->load->model('User_center_model');
		$student = $this->User_center_model->get_student_by_class($id);
		$this->smarty->assign("student", $student);
		$this->smarty->assign("menu", "classes");
		$this->smarty->display("user/class_detail.html");
	}
	
	public function homework()
	{
		//获取作业
		$this->load->model('User_center_model');
		$homework = $this->User_center_model->get_homework_by_uid($this->user_data['id']);
		$this->smarty->assign("homework", $homework);
		$this->smarty->assign("menu", "homework");
		$this->smarty->display("user/homework.html");
	}
	
	public function my_questions(){
		$this->load->model('User_center_model');
		$homework = $this->User_center_model->get_my_questions($this->user_data['id']);
		$this->smarty->assign("homework", $homework);
		$this->smarty->assign("menu", "homework");
		$this->smarty->display("user/my_questions.html");
	}
	
	public function my_answers(){
		$this->load->model('User_center_model');
		$homework = $this->User_center_model->my_answers($this->user_data['id']);
		$this->smarty->assign("homework", $homework);
		$this->smarty->assign("menu", "homework");
		$this->smarty->display("user/my_answers.html");
		
	}
	
	public function my_question_follow(){
		$this->load->model('User_center_model');
		$homework = $this->User_center_model->my_question_follow($this->user_data['id']);
		$this->smarty->assign("homework", $homework);
		$this->smarty->assign("menu", "homework");
		$this->smarty->display("user/my_question_follow.html");
		
	}

	function checkin(){
		$post_data = $this->input->post();
		$result = array();
		$result['error'] = 0;
		$result['msg'] = '签到成功';
		if(empty($post_data['plan_id'])){
			$result['error'] = 1;
			$result['msg'] = '缺少课程ID';
			echo json_encode($result);exit;
		}
		if($post_data['distance'] > 10000){
			$result['error'] = 1;
			$result['msg'] = '距离教室的距离大于100米，无法签到';
			echo json_encode($result);exit;
		}
		$user_student_info = get_field_by_where("o2o_student", "student_id", "user_id='{$this->user_data['id']}'");
		if(empty($user_student_info['student_id'])){
			$result['error'] = 1;
			$result['msg'] = '不是学生无法签到';
			echo json_encode($result);exit;
		}

		$this->load->model('Offline_class_model');
		$plan_info = $this->Offline_class_model->get_class_plan($this->user_data['id'], $post_data['plan_id']);
		if(empty($plan_info)){
			$result['error'] = 1;
			$result['msg'] = '不是您的班级无法签到';
			echo json_encode($result);exit;
		}

		$plan_info = $plan_info[0];
		// print_r($plan_info);
		$class_time_str = explode("-", $plan_info['class_time_str']);
		if(!empty($class_time_str)){
			$plan_last_checkin_time = strtotime($plan_info['class_date'] . ' ' . $class_time_str[1] . ":00");	
		}else{
			$plan_last_checkin_time = strtotime($plan_info['class_time']);
		}
		$time = time();
		//判断签到时间是否已超过最后签到时间
		if($plan_last_checkin_time < $time){
			$result['error'] = 1;
			$result['msg'] = '签到时间已过';
			echo json_encode($result);exit;
		}
		//判断是否开课前半小时
		if(strtotime($plan_info['class_time']) - $time > 1800){
			$result['error'] = 1;
			$result['msg'] = '请于上课前半小时签到';
			echo json_encode($result);exit;
		}
		$this->load->model('User_model');
		$checkin_info = $this->User_model->get_student_checkin_status($this->school_id, $plan_info['class_id'], $user_student_info['student_id'], $this->user_data['id'], $post_data['plan_id'], $plan_last_checkin_time);
		if(!empty($checkin_info['id'])){
			$result['error'] = 1;
			$result['msg'] = '不能重复签到';
			echo json_encode($result);exit;
		}
		$checkin_data['school_id'] 		= $this->school_id;
		$checkin_data['class_id'] 		= $plan_info['class_id'];
		$checkin_data['student_id'] 	= $user_student_info['student_id'];
		$checkin_data['user_id'] 		= $this->user_data['id'];
		$checkin_data['plan_id'] 		= $post_data['plan_id'];
		$checkin_data['checkin_time'] 	= $time;
		$checkin_data['longitude'] 		= $post_data['user_longitude'];
		$checkin_data['latitude'] 		= $post_data['user_latitude'];
		$this->db->insert("o2o_student_checkin", $checkin_data);
		if($this->db->insert_id() > 0){
			$result['error'] = 0;
			$result['msg'] = '签到成功';
			echo json_encode($result);exit;
		}else{
			$result['error'] = 1;
			$result['msg'] = '签到失败，请重试';
			echo json_encode($result);exit;
		}
	}

}
